package io.sk.service.impl;

import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import io.sk.common.base.BaseResult;
import io.sk.common.base.Constants;
import io.sk.common.utils.CheckDataUtils;
import io.sk.dao.ChildDao;
import io.sk.dto.ReviewPageDto;
import io.sk.entity.ChildEntity;
import io.sk.entity.ClassEntity;
import io.sk.entity.PersonnelEntity;
import io.sk.service.ChildService;
import io.sk.service.ClassService;
import io.sk.service.PersonnelService;
import io.sk.vo.ReviewSearchVo;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 学生信息表
 *
 * @author xubinren
 * @version 1.0
 * @date 2018/5/8 16:36
 */
@Service("childService")
public class ChildServiceImpl extends ServiceImpl<ChildDao, ChildEntity> implements ChildService {

    @Autowired
    private ChildDao childDao;

    @Autowired
    private ClassService classService;

    @Autowired
    private PersonnelService personnelService;


    /**
     * 申请加入班级
     *
     * @param child  参数信息
     * @param avatar 头像
     * @return BaseResult
     */
    @Override
    public BaseResult save(ChildEntity child, String avatar) {

        //数据验证
        BaseResult baseResult = checkChild(child);
        if (Constants.RESPONSE_CODE_200 != baseResult.getCode()) {
            return baseResult;
        }
        child = (ChildEntity) baseResult.getData();

        child.preInsert(child.getParentId());
        //申请加入班级状态：1、待审核，2、审核通过，3、审核拒绝
        child.setStatus(Constants.JOIN_STATUS_PENDING);

        //孩子创建
        boolean bl = this.insert(child);
        if (!bl) {
            return CheckDataUtils.sendResult605();
        }

        //给亲属添加头像
        PersonnelEntity personnel = personnelService.selectById(child.getParentId());
        personnel.preUpdate(child.getParentId());
        personnel.setAvatar(avatar);

        bl = personnelService.updateById(personnel);
        if (!bl) {
            return CheckDataUtils.sendResult605();
        }

        return CheckDataUtils.sendResult200();
    }

    /**
     * 分页查询审核列表
     *
     * @param reviewSearchVo 查询条件
     * @return BaseResult
     */
    @Override
    public BaseResult reviewPage(ReviewSearchVo reviewSearchVo) {

        //参数验证
        reviewSearchVo.preInsert();

        //分页信息初始化
        Page<ReviewPageDto> page = new Page(reviewSearchVo.getCurrPage(), reviewSearchVo.getPageSize());

        //分页列表查询
        List<ReviewPageDto> list = childDao.reviewPage(reviewSearchVo, page);
        page.setRecords(list);

        return CheckDataUtils.sendResult200(page);
    }

    /**
     * 加入班级参数验证
     *
     * @param child 参数信息
     * @return BaseResult
     */
    private BaseResult checkChild(ChildEntity child) {

        String middleStr;

        //宝贝姓名
        middleStr = CheckDataUtils.stringTrim(child.getChildName());
        if (StringUtils.isEmpty(middleStr)) {
            return CheckDataUtils.sendResult400("未找到宝贝名称");
        }
        child.setChildName(middleStr);

        //所属班级id
        middleStr = CheckDataUtils.stringTrim(child.getClassId());
        if (StringUtils.isEmpty(middleStr)) {
            return CheckDataUtils.sendResult400("未找到所属班级");
        }
        child.setClassId(middleStr);
        //验证班级
        ClassEntity classEn = classService.selectById(child.getClassId());
        if (null == classEn) {
            return CheckDataUtils.sendResult602("未找到对应的班级信息");
        }

        //亲属id
        middleStr = CheckDataUtils.stringTrim(child.getParentId());
        if (StringUtils.isEmpty(middleStr)) {
            return CheckDataUtils.sendResult400("未找到亲属主键");
        }
        child.setParentId(middleStr);
        //亲属信息
        PersonnelEntity personnel = personnelService.selectById(child.getParentId());
        if (null == personnel) {
            return CheckDataUtils.sendResult602("未找到对应的亲属信息");
        }

        //亲子关系id
        middleStr = CheckDataUtils.stringTrim(child.getRelativeId());
        if (StringUtils.isEmpty(middleStr)) {
            return CheckDataUtils.sendResult400("未找到亲属关系");
        }
        child.setRelativeId(middleStr);

        return CheckDataUtils.sendResult200(child);
    }

}
